using System;
using CodeAngel.Domain.Framework;

namespace CodeAngel.Domain
{
	public partial class AggregateDef
	{
		#region SetDefaultPropertyValues

        /// <summary>
        /// Sets the properties of a new entity to its default values.
        /// </summary>
		internal override void SetDefaultPropertyValues()
		{
            this.SetFriendlyIdAsync();
            this.AggregateDefID = Guid.NewGuid();
            
            if (this.ParentEntity is EntityDef)
                this.EntityDef = this.ParentEntity as EntityDef;
            
            //this.Name = "new";

            this.DataElement = new DataElement(this.Aggregate, this);
            this.DataElement.InitializeAllProperties();
            this.DataElement.SetDefaultPropertyValues();
		}

		#endregion

        #region StoredProcedureName

        /// <summary>
        /// Gets the name for the Get stored procedure, used to populate this aggregate.
        /// </summary>
        public string StoredProcedureName
        {
            get { return "Get_" + this.EntityDef.Name + "__" + this.Name; }
        }

        #endregion

        #region GetUniqueEntities

        /// <summary>
        /// To identify all the unique Entities within this aggregate.  The Entity that 
        /// owns this aggregate is not included in this collection.
        /// </summary>
        /// <returns></returns>
        public EntityDefCollection GetUniqueEntities()
        {
            return this.DataElement.DataElements.GetUniqueEntities();
        }

        #endregion

        #region GetRelatedDataElements

        /// <summary>
        /// To identify all the data elements whose data type Entity is the identified Entity.
        /// </summary>
        public DataElementCollection GetRelatedDataElements(EntityDef thisED)
        {
            return this.DataElement.DataElements.GetRelatedDataElements(thisED);
        }

        #endregion

        #region IncludeAllScalarTypeProperties

        /// <summary>
        /// To identify if any data element within this View has requested that all of its scalar 
        /// type properties should be returned.
        /// </summary>
        public bool IncludeAllScalarTypeProperties(EntityDef thisED)
        {
            foreach (var ThisDE in this.DataElement.DataElements.GetAll())
            {
                if (ThisDE.IsEntityOrCollectionType)
                {
                    if (ThisDE.PropertyDef.DataTypeEntityDef.Name == thisED.Name)
                    {
                        if (ThisDE.IncludeAllScalarTypeProperties)
                            return true;
                    }
                }

            }
            return false;
        }

        #endregion

        #region FileName, ClassName, Directory, Namespace

        public string FileName
        {
            get { return this.EntityDef.Name + this.Name + "Aggregate.cs"; }
        }

        public string ClassName
        {
            get { return this.EntityDef.Name + this.Name + "Aggregate"; }
        }

        public string Directory
        {
            //get { return this.EntityDef.Directory; }
            get { return this.EntityDef.EntitiesDirectoryPath; }
        }

        public string Namespace
        {
            get { return this.EntityDef.Domain.AggregatesNamespace; }
        }

        #endregion

        #region GetRootDataTypeAsString

        /// <summary>
        /// Gets the data type for the Root property.
        /// </summary>
        public string GetRootDataTypeAsString()
        {
            if (this.RootIsCollection)
                return this.EntityDef.ClassNames.Collection();
            else
                return this.EntityDef.ClassNames.ClassName();
        }

        #endregion

        /// <summary>
        /// Gets all the unique entity definitions in this view that are flagged as updateable
        /// </summary>
        public EntityDefCollection GetUpdateableEntities()
        {
            var items = new EntityDefCollection(null);

            if (this.DataElement.IsUpdateable)
                items.Add(this.EntityDef);
            foreach (DataElement ThisDE in this.DataElement.DataElements.GetAll().EntityOrCollectionTypes)
            {
                if (ThisDE.IsUpdateable)
                    items.Add(ThisDE.PropertyDef.DataTypeEntityDef);
            }

            return items;
        }
    }
}

